<template>
     <div class="components-container">
         <div class="header-form">
            <div class="layui-inline">
                  <label class="layui-form-label" >开始日期</label> 
                  <el-date-picker v-date v-model="selectData.CrhbegdateSta" type="date" placeholder="大于等于该日期" style="width: 200px">
                  </el-date-picker>     
                  <el-date-picker v-date v-model="selectData.CrhbegdateEnd" type="date" placeholder="小于等于该日期" style="width: 200px">
                  </el-date-picker>
            </div>
            <el-button type="primary" :disabled="downloadLoading" @click="btnSearch" :loading="this.$store.state.isloading" >搜索</el-button>
            <el-button :disabled="this.$store.state.isloading || downloadLoading"  @click="btnRepeat">重置</el-button>
        </div>
        <div>
          <ul class="toolList"> 
            <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
              <el-button type="primary" :disabled="$store.state.isloading" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
            </li>
            <li>
            <el-button type="primary" :disabled="this.$store.state.isloading"  @click="exportExcel" style="margin-left:20px;margin-bottom:-10px;">导出Excel</el-button>
            </li>
          </ul>
        </div>
        <dataTable :headList="headList" :dataList="dataList" :isIndex="true" :pageOrder="pageOrder" :hiddenPageOrder="false" :height="height" @pageChange="pageChange" @selectRow="selectRow"></dataTable>
        <!-- <ich ref="sweetModalMain"  @refreshData="getData"></ich> -->
        <printmodal ref="printPdf"></printmodal>
        <!-- <cresimplereg ref="simpleReg" :orderType="order" @refreshData="getData"></cresimplereg> -->
     </div>
</template>

<script>
import dataTable from "@/components/Table/DataTable";
// import ich from "./iceuseDetail";

import apiBase from "@/api/apiBase.js";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { manage } from "@/mixins/managePage.js";
import printmodal from "@/components/Pdf/Print";
import { PRINT } from "@/components/Pdf/PDF.js";
// import cresimplereg from "../businessHall/cresimplereg";
const INFO = "详情";
const EXPORT = "导出";
const CRDETAILS = "火化登记详情";
export default {
  name: "iceuseSearch",
  mixins: [manage],
  data() {
    return {
      all: null,
      selectData: {
        IchstatusEqual: null,
        RegbusnumEquals: null,
        CrhbegdateSta: null,
        CrhbegdateEnd: null
      },
      usetype: [],
      activeName: "results",
      height: "620",
      downloadLoading:false
    };
  },
  methods: {
    exportExcel() {
      if (this.dataList.length === 0){
          this.$message.error("无导出数据！");
          return;
      }
      var startTime;
      var endTime;
      if(this.selectData.CrhbegdateSta){
        startTime = this.selectData.CrhbegdateSta.toLocaleDateString();
      }else{
        startTime = '空';
      }
      if(this.selectData.CrhbegdateEnd){
        endTime = this.selectData.CrhbegdateEnd.toLocaleDateString();
      }else{
        endTime = '空';
      }
    //    let headers = [];
    //   for(var i = 0;i < this.headList.length;i++){
    //     if(this.headList[i].ColumnChild == null){
    //         headers.push(this.headList[i]);
    //     }else{
    //       for(var s = 0;s <this.headList[i].ColumnChild.length;s++){
    //         headers.push(this.headList[i].ColumnChild[s]);
    //       }
    //     }
    //   }
      let useState;
    useState = '火化记录查询' + '(' + startTime + '--' + endTime + ')';
 
    console.log(1254);
        console.log(this.headList);
      
      this.downloadLoading = true;
      require.ensure([], () => {
        const { export_json_to_excel } = require("@/vendor/Export2Excel");
        export_json_to_excel(this.headList, this.dataList, useState);
        this.downloadLoading = false;
      });
    },
    //功能操作
    operate(order) {
      this.order = order;
      switch (this.order) {
        case INFO:
        //   this.$refs.sweetModalMain.open(this.order, this.currentRow.ICHID);
            alert('未完待续!')
            break;
        case EXPORT:
          this.$refs.printPdf.report(PRINT.PDFICEDISPATCHSEARCH,this.getDataC());
          break;
        case CRDETAILS:
          this.$refs.simpleReg.open('详情', this.currentRow.REGID);
          break;
        default:
      }
    },
    getColumns() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Column.GetCremationSumColumns,
        res => {
          this.headList = res.data.content;
        }
      );
    },
    getDataC(){
      let Data = JSON.parse(JSON.stringify(this.selectData));
    //   if (!this.selectData.RegbusnumEquals) {
    //     Data.RegbusnumEquals = null;
    //   }
      Data.CrhbegdateSta = formatDate(
        Data.CrhbegdateSta,
        "yyyyMMdd"
      );
      Data.CrhbegdateEnd = formatDate(
        Data.CrhbegdateEnd,
        "yyyyMMdd"
      );
      let data = {
        PageIndex: -1,
        PageSize: 0,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      return data;
    },
    getData() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      Data.CrhbegdateSta = formatDate(
        Data.CrhbegdateSta,
        "yyyyMMdd"
      );
      Data.CrhbegdateEnd = formatDate(
        Data.CrhbegdateEnd,
        "yyyyMMdd"
      );
      let data = {
        PageIndex: -1,
        PageSize: 0,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      apiBase.DoService(data, apiBase.getApiAdress().Report.QueryCremationSum, res => {
        this.dataList = res.data.content.OutData;
        console.log(this.dataList);
        this.activeName = "results";
        this.pageOrder.totalNum = res.data.content.RecordCount;
      });
    },
    //获取业务状态
    getIchStatus() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().IceUse.GetIchStatus,
        res => {
          this.usetype = res.data.content;
        }
      );
    },
    btnShow(btn){
      switch (btn) {
          case '导出':
            return true;
            break;
          case '开放':
            return false;
            break;
          default: return false;
        }
    },
    //查询条件重置
    btnRepeat() {
      this.selectData = {
        IchstatusEqual: null,
        RegbusnumEquals: null,
        CrhbegdateSta: null,
        CrhbegdateEnd: null
      };
      this.selectData.CrhbegdateSta = new Date(new Date().setDate(1));
      // this.selectData.CrhbegdateSta.setDate(1);
      this.selectData.CrhbegdateEnd = new Date();
      var day = new Date();
      var year = day.getFullYear();
      var month = day.getMonth()+1;
      var d = new Date(year, month, 0);
      var dat = d.getDate();
      // this.selectData.CrhbegdateEnd.setDate(dat);
      this.getData();
    }
  },
  mounted() {
    // console.log(this.all);
    this.selectData.IchstatusEqual = "A";
  },
  components: {
    dataTable,
    printmodal
  },
  created() {
    this.selectData.CrhbegdateSta = new Date(new Date().setDate(1));
    // this.selectData.CrhbegdateSta.setDate(1);
    this.selectData.CrhbegdateEnd = new Date();
    this.selectData.IchstatusEqual = "A";
    var day = new Date();
    var year = day.getFullYear();
    var month = day.getMonth()+1;
    var d = new Date(year, month, 0);
    var dat = d.getDate();
    // this.selectData.CrhbegdateEnd.setDate(dat);
    this.getColumns();//获取headerlist
    this.getData();//获取数据
    this.getIchStatus();//获取业务状态
  }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
</style>

